package com.huike.web.controller.common;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.service.Impl.SysFileServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 *
 */

/**
 * author:
 * Date:2022/4/118:29
 */
@Slf4j
@RestController

public class CommonController {
    @Autowired
    private SysFileServiceImpl fileService;

    @GetMapping("/common/download")
    public void download(String fileName, boolean dalete, HttpServletResponse response) throws IOException {
        /*// 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName );*/

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        EasyExcel.write(response.getOutputStream(), TbClueExcelVo.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("模板")
                .doWrite((List) data());

    }
    private Collection<?> data1() {

        List<TbClue> list = new ArrayList<>();
        return list;
    }

    private Collection<?> data() {

        List<TbClue> list = new ArrayList<>();
        return list;
    }
    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception{
        try{
            // 上传文件路径
            return fileService.upload(file);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     * @param delete 是否删除

    @GetMapping("/common/download")
    public AjaxResult fileDownload(String fileName, HttpServletResponse response, HttpServletRequest request){
        try{
            if (!FileUtils.checkAllowDownload(fileName)){
                throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", fileName));
            }
            return fileService.downloadByMinio(fileName,response);
        }catch (Exception e){
            log.error("下载文件失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }
     */

}
